local zhouxuan = fk.CreateSkill {

  name = "joysp__zhouxuan",

  tags = {  },

}



zhouxuan:addEffect(fk.CardUsing, {
  name = "joysp__zhouxuan",
  anim_type = "drawcard",
  expand_pile = "joy__xuan",
  events = {fk.CardUsing,fk.CardResponding,fk.EventPhaseStart,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhouxuan.name) then
      if event == fk.CardUsing or event == fk.CardResponding then
        return true
      elseif event == fk.EventPhaseStart then
        return player.phase == Player.Discard and #player:getPile("joy__xuan") < 5 and not player:isKongcheng()
      elseif event == fk.CardUseFinished then
        return player:getMark("joy__zhouxuan") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if  #player:getPile("joy__xuan") == 0 and event == fk.CardUsing  then
      player.room:setPlayerMark(player,"joy__zhouxuan",1)
    end
      if event == fk.EventPhaseStart then
        local x = 5 - #player:getPile("joy__xuan")
        local cards = player.room:askForCard(player, 1, x, false, zhouxuan.name, true,
        ".", "#joy__zhouxuan-invoke:::".. tostring(x))
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      elseif event == fk.CardUsing or event == fk.CardResponding then
        return #player:getPile("joy__xuan") > 0
      elseif event == fk.CardUseFinished then
        player.room:setPlayerMark(player,"joy__zhouxuan",0)
        if #player:getPile("joy__xuan") == 0 and not data.card:isVirtual() and player.room:getCardArea(data.card) == Card.Processing then
          return player.room:askForSkillInvoke(player,zhouxuan.name,data,"#joy__zhouxuan-invoke2")
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing or event == fk.CardResponding then
        local n = 1
        local x = 1
        if not table.every(room:getOtherPlayers(player), function(p)
         return #p.player_cards[Player.Hand] < #player.player_cards[Player.Hand] end) and #player:getPile("joy__xuan") > 1 then
          n = #player:getPile("joy__xuan")
          x = 2
        end
        player:drawCards(n,zhouxuan.name)
        local cids = room:askForCard(player, x, x, false, zhouxuan.name, false, ".|.|.|joy__xuan|.|.", "#joy__zhouxuan-card:::"..x, "joy__xuan")
        if #player:getPile("joy__xuan") == 0 then return false end
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, zhouxuan.name, "joy__xuan")
    elseif event == fk.EventPhaseStart then
      player:addToPile("joy__xuan", event:getCostData(self), false, zhouxuan.name)
    elseif event == fk.CardUseFinished then
      player:addToPile("joy__xuan", data.card, false, zhouxuan.name)
    end
  end,
})
zhouxuan:addEffect(fk.CardResponding, {
  name = "joysp__zhouxuan",
  anim_type = "drawcard",
  expand_pile = "joy__xuan",
  events = {fk.CardUsing,fk.CardResponding,fk.EventPhaseStart,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhouxuan.name) then
      if event == fk.CardUsing or event == fk.CardResponding then
        return true
      elseif event == fk.EventPhaseStart then
        return player.phase == Player.Discard and #player:getPile("joy__xuan") < 5 and not player:isKongcheng()
      elseif event == fk.CardUseFinished then
        return player:getMark("joy__zhouxuan") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if  #player:getPile("joy__xuan") == 0 and event == fk.CardUsing  then
      player.room:setPlayerMark(player,"joy__zhouxuan",1)
    end
      if event == fk.EventPhaseStart then
        local x = 5 - #player:getPile("joy__xuan")
        local cards = player.room:askForCard(player, 1, x, false, zhouxuan.name, true,
        ".", "#joy__zhouxuan-invoke:::".. tostring(x))
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      elseif event == fk.CardUsing or event == fk.CardResponding then
        return #player:getPile("joy__xuan") > 0
      elseif event == fk.CardUseFinished then
        player.room:setPlayerMark(player,"joy__zhouxuan",0)
        if #player:getPile("joy__xuan") == 0 and not data.card:isVirtual() and player.room:getCardArea(data.card) == Card.Processing then
          return player.room:askForSkillInvoke(player,zhouxuan.name,data,"#joy__zhouxuan-invoke2")
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing or event == fk.CardResponding then
        local n = 1
        local x = 1
        if not table.every(room:getOtherPlayers(player), function(p)
         return #p.player_cards[Player.Hand] < #player.player_cards[Player.Hand] end) and #player:getPile("joy__xuan") > 1 then
          n = #player:getPile("joy__xuan")
          x = 2
        end
        player:drawCards(n,zhouxuan.name)
        local cids = room:askForCard(player, x, x, false, zhouxuan.name, false, ".|.|.|joy__xuan|.|.", "#joy__zhouxuan-card:::"..x, "joy__xuan")
        if #player:getPile("joy__xuan") == 0 then return false end
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, zhouxuan.name, "joy__xuan")
    elseif event == fk.EventPhaseStart then
      player:addToPile("joy__xuan", event:getCostData(self), false, zhouxuan.name)
    elseif event == fk.CardUseFinished then
      player:addToPile("joy__xuan", data.card, false, zhouxuan.name)
    end
  end,
})
zhouxuan:addEffect(fk.EventPhaseStart, {
  name = "joysp__zhouxuan",
  anim_type = "drawcard",
  expand_pile = "joy__xuan",
  events = {fk.CardUsing,fk.CardResponding,fk.EventPhaseStart,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhouxuan.name) then
      if event == fk.CardUsing or event == fk.CardResponding then
        return true
      elseif event == fk.EventPhaseStart then
        return player.phase == Player.Discard and #player:getPile("joy__xuan") < 5 and not player:isKongcheng()
      elseif event == fk.CardUseFinished then
        return player:getMark("joy__zhouxuan") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if  #player:getPile("joy__xuan") == 0 and event == fk.CardUsing  then
      player.room:setPlayerMark(player,"joy__zhouxuan",1)
    end
      if event == fk.EventPhaseStart then
        local x = 5 - #player:getPile("joy__xuan")
        local cards = player.room:askForCard(player, 1, x, false, zhouxuan.name, true,
        ".", "#joy__zhouxuan-invoke:::".. tostring(x))
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      elseif event == fk.CardUsing or event == fk.CardResponding then
        return #player:getPile("joy__xuan") > 0
      elseif event == fk.CardUseFinished then
        player.room:setPlayerMark(player,"joy__zhouxuan",0)
        if #player:getPile("joy__xuan") == 0 and not data.card:isVirtual() and player.room:getCardArea(data.card) == Card.Processing then
          return player.room:askForSkillInvoke(player,zhouxuan.name,data,"#joy__zhouxuan-invoke2")
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing or event == fk.CardResponding then
        local n = 1
        local x = 1
        if not table.every(room:getOtherPlayers(player), function(p)
         return #p.player_cards[Player.Hand] < #player.player_cards[Player.Hand] end) and #player:getPile("joy__xuan") > 1 then
          n = #player:getPile("joy__xuan")
          x = 2
        end
        player:drawCards(n,zhouxuan.name)
        local cids = room:askForCard(player, x, x, false, zhouxuan.name, false, ".|.|.|joy__xuan|.|.", "#joy__zhouxuan-card:::"..x, "joy__xuan")
        if #player:getPile("joy__xuan") == 0 then return false end
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, zhouxuan.name, "joy__xuan")
    elseif event == fk.EventPhaseStart then
      player:addToPile("joy__xuan", event:getCostData(self), false, zhouxuan.name)
    elseif event == fk.CardUseFinished then
      player:addToPile("joy__xuan", data.card, false, zhouxuan.name)
    end
  end,
})
zhouxuan:addEffect(fk.CardUseFinished, {
  name = "joysp__zhouxuan",
  anim_type = "drawcard",
  expand_pile = "joy__xuan",
  events = {fk.CardUsing,fk.CardResponding,fk.EventPhaseStart,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(zhouxuan.name) then
      if event == fk.CardUsing or event == fk.CardResponding then
        return true
      elseif event == fk.EventPhaseStart then
        return player.phase == Player.Discard and #player:getPile("joy__xuan") < 5 and not player:isKongcheng()
      elseif event == fk.CardUseFinished then
        return player:getMark("joy__zhouxuan") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if  #player:getPile("joy__xuan") == 0 and event == fk.CardUsing  then
      player.room:setPlayerMark(player,"joy__zhouxuan",1)
    end
      if event == fk.EventPhaseStart then
        local x = 5 - #player:getPile("joy__xuan")
        local cards = player.room:askForCard(player, 1, x, false, zhouxuan.name, true,
        ".", "#joy__zhouxuan-invoke:::".. tostring(x))
        if #cards > 0 then
          event:setCostData(self, cards)
          return true
        end
      elseif event == fk.CardUsing or event == fk.CardResponding then
        return #player:getPile("joy__xuan") > 0
      elseif event == fk.CardUseFinished then
        player.room:setPlayerMark(player,"joy__zhouxuan",0)
        if #player:getPile("joy__xuan") == 0 and not data.card:isVirtual() and player.room:getCardArea(data.card) == Card.Processing then
          return player.room:askForSkillInvoke(player,zhouxuan.name,data,"#joy__zhouxuan-invoke2")
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing or event == fk.CardResponding then
        local n = 1
        local x = 1
        if not table.every(room:getOtherPlayers(player), function(p)
         return #p.player_cards[Player.Hand] < #player.player_cards[Player.Hand] end) and #player:getPile("joy__xuan") > 1 then
          n = #player:getPile("joy__xuan")
          x = 2
        end
        player:drawCards(n,zhouxuan.name)
        local cids = room:askForCard(player, x, x, false, zhouxuan.name, false, ".|.|.|joy__xuan|.|.", "#joy__zhouxuan-card:::"..x, "joy__xuan")
        if #player:getPile("joy__xuan") == 0 then return false end
        room:moveCardTo(cids, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, zhouxuan.name, "joy__xuan")
    elseif event == fk.EventPhaseStart then
      player:addToPile("joy__xuan", event:getCostData(self), false, zhouxuan.name)
    elseif event == fk.CardUseFinished then
      player:addToPile("joy__xuan", data.card, false, zhouxuan.name)
    end
  end,
})

return zhouxuan